<template>
  <div class="user-management">
    <!-- 搜索区域 -->
    <div class="search-area">
      <div class="search-inputs">
        <el-input
          v-model="searchParams.belongArea"
          placeholder="请输入所属辖区"
          clearable
          style="width: 200px; margin-right: 10px;"
        />
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </div>
    </div>

    <!-- 贫困户表格 -->
    <div class="table-container">
      <el-table :data="pagedClue" border style="width: 100%; margin-top: 20px;">
         <el-table-column prop="clueId" label="线索编号" width="100" />
        <el-table-column prop="familyId" label="家庭编号" width="100" />
        <el-table-column prop="belongArea" label="所属辖区" width="180" />
        <el-table-column prop="riskType" label="风险类型" width="180" />
        <el-table-column prop="riskLevel" label="风险等级" width="100" />
        <el-table-column prop="riskRemark" label="风险备注" width="180" />
        <el-table-column prop="helpGuidance" label="帮扶指导" width="180" />
        <el-table-column prop="currentStage" label="线索状态" width="100">
        </el-table-column>
        <el-table-column label="操作" width="230">
          <template #default="{ row }">
            <div class="action-buttons">
            <el-button type="success" size="small" @click="showEditDialog(row)">核实</el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)">线索注销</el-button>
          </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        v-model="currentPage"
        :page-size="pageSize"
        layout="prev, pager, next"
        :total="totalItems">
      </el-pagination>
    </div>
    <!-- 编辑线索状态对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px" :before-close="handleCloseDialog">
      <el-form :model="clueForm" label-width="100px">
        <el-form-item label="所属辖区" required>
          <el-input v-model="clueForm.belongArea"  disabled/>
        </el-form-item>
        <el-form-item label="风险类型" required>
          <el-input v-model="clueForm.riskType" disabled/>
        </el-form-item>
        <el-form-item label="风险等级" required>
          <el-input v-model="clueForm.riskLevel"  disabled/>
        </el-form-item>
        <el-form-item label="风险备注" required>
          <el-input v-model="clueForm.riskRemark" disabled/>
        </el-form-item>
        <el-form-item label="帮扶指导" required>
          <el-input v-model="clueForm.helpGuidance" disabled/>
        </el-form-item>
        <el-form-item label="线索状态" required>
          <el-input v-model="clueForm.currentStage" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handleCloseDialog">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getClueTables,updateClueTables,deleteClueTable } from '@/api/clue'
    // 分页参数
    const currentPage = ref(1)
    const pageSize = ref(10)
    const pagedClue=ref([])
    // 搜索参数
    const searchParams = reactive({
      belongArea: ''
    })

    const totalItems=ref(0)
    

    // 查询当前表格中的数据
const queryClue = () => {
  getClueTables(searchParams).then((res) => {
    if (res.code == 2000) {
      pagedClue.value = res.data
      totalItems.value = res.total
    } else {
      pagedClue.value = []
      totalItems.value = 0
    }
  })
}
// 页面加载的时候，直接就调用后台，获取表格中的数据
queryClue()

const dialogVisible=ref(false)
const isEditing=ref(false)
const dialogTitle='修改线索信息'

const clueForm=reactive({
  clueId:'',
  familyId:'',
  belongArea:'',
  riskType:'',
  riskLevel:'',
  riskRemark:'',
  helpGuidance:'',
  currentPage:''
})

// 显示编辑对话框
const showEditDialog = (row) => {
  isEditing.value = true
  Object.assign(clueForm, JSON.parse(JSON.stringify(row)))
  dialogVisible.value = true
}

const resetForm=()=>{
  Object.assign(clueForm,{
    currentStage
  })
}

// 关闭对话框
const handleCloseDialog = () => {
  dialogVisible.value = false
}

    // 处理搜索
    const handleSearch = () => {
      queryClue()
    }

    // 处理分页大小变化
    const handleSizeChange = (val) => {
      pageSize.value = val
      handleSearch()
    }

    // 处理当前页变化
    const handleCurrentChange = (val) => {
      currentPage.value = val
      handleSearch()
    }

    //处理修改
    const handleConfirm=()=>{
      if(!clueForm.riskRemark){
        ElMessage.warning('请填写风险备注')
        return
      }
      updateClueTables(clueForm).then(res=>{
        if(res.code==2000){
          ElMessage.success('修改成功')
          handleSearch()
        }
      })
      dialogVisible.value = false
    }

    // 处理销号
  const handleDelete = (row) => {
  ElMessageBox.confirm('是否对该线索进行销号?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteClueTable(row.clueId).then(res => {
      ElMessage.success('删除成功')
      handleSearch() // 重新筛选数据
    })

  })
}

    
 
</script>

<style scoped>
/* 表格样式 */
.el-table__header .cell {
  text-align: center !important;
  font-weight: bold;
}

.user-management {
  padding: 20px;
  min-height: 100vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.search-area {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 20px;
  max-width: 1250px;
}

.search-inputs {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

/* 确保按钮文字居中 */
.el-button {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

/* 让表格中所有单元格的文字居中 */
.el-table .cell {
  text-align: center !important;
}

/* 操作按钮样式 */
.action-buttons {
  display: flex;
  justify-content: center;
  gap: 10px;
  align-items: center;
  text-align: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  width: 1250px;
  max-width: 1250px;
}

.table-container {
  width: 100%;
  max-width: 1250px;
  display: flex;
  justify-content: center;
}

.el-table {
  min-width: 100%;
  table-layout: auto;
}

.action-buttons {
  display: flex; /* 使用flex布局 */
  justify-content: center; /* 水平居中按钮 */
  gap: 10px; /* 按钮间间距 */
  align-items: center; /* 垂直居中按钮 */
}

/* 响应式布局 */
@media (max-width: 768px) {
  .search-inputs {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .el-table {
    font-size: 14px;
  }
}
</style>